﻿
#region 作者和版权
/*************************************************************************************
* CLR 版本: 4.0.30319.42000
* 类 名 称: ExecutorPool
* 机器名称: SUNTIA-QL
* 命名空间: ServerFramework.Thread
* 文 件 名: ExecutorPool
* 创建时间: 2015/9/21 17:29:39
* 作 者: 乔龙 Qiaolong
* 版 权: ExecutorPool说明：本代码版权归乔龙所有，使用时必须带上乔龙网站地址 All Rights Reserved (C) 2015 - 2013
* 签 名: To be or not, it is not a problem !
* 网 站: http://www.suntia.net
* 邮 箱: qiaolong306@qq.com 
* 唯一标识： 318b7790-1a18-4e3f-a899-ed14d29eef39 
*
* 登录用户: simida
* 所 属 域: SUNTIA-QL

* 创建年份: 2015
* 修改时间:
* 修 改 人:
* 
************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;


namespace ServerFramework.ThreadUtil
{
    /// <summary>
    /// 一个单线程方式处理
    /// </summary>
    public class SingleExecutor
    {
        private Mutex _mutex = new Mutex();
        private static SingleExecutor _instance;
        public static SingleExecutor Instance { get { if (_instance == null)_instance = new SingleExecutor(); return _instance; } }

        /// <summary>
        /// 单线程处理逻辑
        /// </summary>
        /// <param name="d"></param>
        public void Execute(Action d)
        {
            lock (this)
            {
                //互斥加锁
                _mutex.WaitOne();
                try
                {
                    d();
                }
                finally
                {
                    //释放互斥体
                    _mutex.ReleaseMutex();
                }
            }
        }
    }


    /// <summary>
    /// 一个多线程方式处理
    /// </summary>
    public class MulExecutor
    {
        private static SingleExecutor _instance;
        public static SingleExecutor Instance { get { if (_instance == null)_instance = new SingleExecutor(); return _instance; } }
        /// <summary>
        /// 单线程处理逻辑
        /// </summary>
        /// <param name="d"></param>
        public void Execute(Action d)
        {
            Task.Factory.StartNew(d);
        }
    }
}


